var gulp=require("gulp");
var livereload=require("gulp-livereload");
var webserver=require("gulp-webserver");
var sass=require("gulp-ruby-sass");
var uglify = require("gulp-uglify");
var rename=require("gulp-rename");
var htmlmin=require("gulp-htmlmin");
var concat = require("gulp-concat");
var imagemin = require("gulp-imagemin")

gulp.task("shuaxin",function () {
    return gulp.src("dest")
        .pipe(webserver({
            livereload:true,
            open:true
        }))
})

//移动
gulp.task("movefile",function () {
    return gulp.src("src/**/*.{html,png,jpg,gif,icon,jpeg,js}")
        .pipe(gulp.dest("./dest"))
})

//编译sass   gulp-ruby-sass
gulp.task("sass",function () {
    return sass("src/scss/*.scss",{style:"compressed"})
        .on("error",function (err) {
            console.log("scss文件错误")
            console.log(err)
        })
        .pipe(gulp.dest("dest/css"))
})
/*// 压缩html代码
// gulp-htmlmin
gulp.task("htmlmin",function(){
    return gulp.src("src/!**!/!*.html")
        .pipe(htmlmin({
            removeComments: true, // 移除注释
            collapseWhitespace: true // 移除空白内容
        }))
        .pipe(gulp.dest("dest"))
})

// //压缩js文件 gulp-uglify
gulp.task("js",function () {
    return gulp.src("src/js/!*.js")
        .pipe(uglify())
        .pipe(gulp.dest("dest/js"))
})
// 合并第三方库
// gulp-concat
gulp.task("concat",function(){
    return gulp.src("src/lib/!*.js")
        .pipe(concat("lib.js"))
        .pipe(gulp.dest("dest/lib"));
})*/

// 压缩图片
// gulp-imagemin
gulp.task('image',function(){
    return gulp.src('src/img/*.{png,jpg,gif,icon}')
        .pipe(imagemin({progressive: true}))//压缩
        .pipe(gulp.dest('dest/img'))//输出
})
//重命名
// gulp.task("rename",function () {
//     return gulp.src("src/js/*.js")
//         .pipe(rename({suffix:".min"}))
//         .pipe(uglify())
//         .pipe(gulp.dest("dest/js"))
// })

// 监听
gulp.task("watch",function () {
    gulp.watch("src/**/*.{html,png,jpg,gif,icon,jpeg,js}",["movefile"]);
    gulp.watch("src/scss/*.scss",["sass"]);
});

//默认
gulp.task("default",["movefile","image","sass","watch","shuaxin"]);
